<?php

class ApiModule extends CWebModule
{
    public function init()
    {
        $this->setImport(array(
                        'api.models.*',
                        'api.components.*',
        ));
    }

    public function beforeControllerAction($controller, $action)
    {
        
        $mac = Yii::app()->request->getParam('mac');
        $accName = Yii::app()->request->getParam('accName');
        if (is_null($mac) || empty($mac) || is_null($accName) || empty($accName)) {
            $controller->_sendAjaxResponse(json_encode(array('status' => Constants::RESPONSE_NOT_AUTHENTICATE, 'message' => 'Not authenticate')));
        }

        $accName = Security::decrypt($accName, Constants::ENCRIPT_KEY);
        $memberDao = new MembersDAO();
        $member = $memberDao->findByNameAndMac($accName, $mac);

        if (is_null($member)) {
            $controller->_sendAjaxResponse(json_encode(array('status' => Constants::RESPONSE_NOT_AUTHENTICATE, 'message' => 'Not authenticate')));
        }

        Yii::app()->user->setState("memberInfo",$member);
        Yii::app()->user->setState("memberId", $member['member_id']);
        Yii::app()->user->setState("nickname", $member['nickname']);
        Yii::app()->user->setState("macAddress", $member['mac_address']);
        Yii::app()->user->setState("memberStatus", $member['member_status']);
        $accounts = $memberDao->findAccountById($member['member_id']);
        $accountList = CHtml::listData($accounts, 'site_id', 'account_id');
        Yii::app()->user->setState("accountList", $accountList);
        Yii::app()->user->setState("accName", $accName);

        if(parent::beforeControllerAction($controller, $action))
        {
            return true;
        }
        else
            return false;
    }

}
